package org.herrdommel.wakemeup.provider;

import org.herrdommel.wakemeup.model.Alarm;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class AlarmDatabaseHelper extends SQLiteOpenHelper {

	/** Database name */
	private static final String DATABASE_NAME = "wakemeup.db";
	/** Database version */
	private static final int DATABASE_VERSION = 1;
	/** Database creation sql statement */
	private static final String CREATE_TABLE_ALARM = "CREATE TABLE " + Alarm.ALARMS_TABLE_NAME + " (" + Alarm._ID
			+ " INTEGER PRIMARY KEY," + Alarm.NAME + " TEXT NOT NULL," + Alarm.TIME + " TEXT NOT NULL,"
			+ Alarm.IS_ACTIVE + " INTEGER NOT NULL," + Alarm.NEEDS_CONFIRMATION + " INTEGER NOT NULL,"
			+ Alarm.ALERT_ON_LOW_BATTERY + " INTEGER NOT NULL" + ")";

	private static final String DB_SCHEMA = CREATE_TABLE_ALARM;

	/**
	 * Default private constructor, see {@link SQLiteOpenHelper}
	 * @param context the context passed to the helper
	 */
	public AlarmDatabaseHelper(final Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	// Method is called during creation of the database
	@Override
	public void onCreate(final SQLiteDatabase database) {
		database.execSQL(DB_SCHEMA);
	}

	// Method is called during an upgrade of the database, e.g. if you increase the database version
	@Override
	public void onUpgrade(final SQLiteDatabase database, final int oldVersion, final int newVersion) {
		Log.w(AlarmDatabaseHelper.class.getName(), "Upgrading database from version " + oldVersion + " to "
				+ newVersion + ", which will destroy all old data");
		database.execSQL("DROP TABLE IF EXISTS " + Alarm.ALARMS_TABLE_NAME);
		onCreate(database);
	}

}
